package org.freedoit.server.service.task;

import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;

import org.freedoit.common.data.FreeDoitReceiveRequest;
import org.freedoit.common.data.FreeDoitSyncDataResponse;
import org.freedoit.common.shared.SharedProject;
import org.freedoit.common.shared.SharedTask;
import org.freedoit.common.util.FreeDoitDataUtils;
import org.freedoit.server.dao.DAOFactory;
import org.freedoit.server.om.Project;
import org.freedoit.server.om.Task;

public class ReceiveTaskServlet extends AbstractTaskServlet<FreeDoitReceiveRequest, FreeDoitSyncDataResponse> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5491581135229913652L;

	protected FreeDoitSyncDataResponse createResponseObject() {
		// TODO Auto-generated method stub
		return new FreeDoitSyncDataResponse();
	}

	protected void processRequest(
			ServiceContext<FreeDoitReceiveRequest, FreeDoitSyncDataResponse> context,
			Long timestamp) {
		// TODO Auto-generated method stub
		FreeDoitReceiveRequest reuqestObject = context.getRequestObject();
		FreeDoitSyncDataResponse responseObject = context.getResponseObject();
		if (reuqestObject == null) {
			responseObject.setCode(FreeDoitSyncDataResponse.CODE_INVALID_REQUEST);
			return;
		}
		
		// Process Task
		Timestamp dateTimestamp = null;
		if (FreeDoitDataUtils.NO_TIMESTAMP_KEY.compareTo(reuqestObject.getTimestamp()) != 0) {
			dateTimestamp = new Timestamp(reuqestObject.getTimestamp());
		}
		
		Timestamp now = new Timestamp(timestamp);
		
		// Projects
		List<Project> latestProjectList = DAOFactory.getProjectDAO().findLatest(context.getCurrentUserId(), 
				context.getCurrentClientId(), dateTimestamp, now);
		if (latestProjectList != null && !latestProjectList.isEmpty()) {
			List<SharedProject> projectResponseList = new LinkedList<SharedProject>();
			for (Project p : latestProjectList) {
				projectResponseList.add(new SharedProject(null, p.getProjectId(), p.getProjectName(), 
						p.getComment(), p.getCreateDate(), p.getStartDate(), p.getDueDate(), p.getStatus()));
			}
			responseObject.setProjects(projectResponseList);
		}
		
		// Task
		List<Task> latestTaskList = DAOFactory.getTaskDAO().findLatest(context.getCurrentUserId(), 
				context.getCurrentClientId(), dateTimestamp, now);
		if (latestTaskList != null && !latestTaskList.isEmpty()) {
			List<SharedTask> taskResponseList = new LinkedList<SharedTask>();
			for (Task t : latestTaskList) {
				taskResponseList.add(new SharedTask(null, t.getTaskId(), t.getTaskTypeId(), t.getProjectId(),
						t.getTaskName(), t.getDescription(), t.getStartDate(), t.getDueDate(), t.getFinishDate(),
						t.getStartTime(), t.getDueTime(), t.getFinishTime(), t.getFinished(), t.getTaskTag(),
						t.getStatus()));
			}
			responseObject.setTasks(taskResponseList);
		}
		
		// Task Type
//		if (dateTimestamp == null) {
//			List<TaskType> latestTaskTypeList = DAOFactory.getTaskTypeDAO().findAll();
//			if (latestTaskTypeList != null && !latestTaskTypeList.isEmpty()) {
//				List<SharedTaskType> taskTypeResponseList = new LinkedList<SharedTaskType>();
//				for (TaskType t : latestTaskTypeList) {
//					taskTypeResponseList.add(new SharedTaskType(null, t.getTaskTypeId(), t.getTaskTypeName(), 
//							t.getTaskTypeDescrition(), t.getStatus()));
//				}
//				responseObject.setTaskTypes(taskTypeResponseList);
//			}
//		}
		
		responseObject.setCode(FreeDoitSyncDataResponse.CODE_OK);
		responseObject.setTimestamp(timestamp);
	}
	
	protected Type getRequestType() {
		return new com.google.gson.reflect.TypeToken<FreeDoitReceiveRequest>() {  
		}.getType();
	}

}
